package com.jishuo.base.util;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

/**
 * 日期工具
 */
public class GTimeUtil implements Serializable {

	private static final long serialVersionUID = -1705094716330245682L;
    public static final int YYYYMMDDhhmmssxxx = 7;

    private static final DateTimeFormatter FullDateTime = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    private static final DateTimeFormatter DateTimeMark = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DateTime = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static final DateTimeFormatter DateMark = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /** 格式：yyyy-MM-dd HH:mm:ss */
    public static String getDateTimeMark() {
        return LocalDateTime.now().format(DateTimeMark);
    }

    /** 格式：yyyyMMddHHmmss */
    public static String getDateTime() {
        return LocalDateTime.now().format(DateTime);
    }

    /** 格式：yyyyMMddHHmmssSSS */
    public static String getFullDateTime() {
        return LocalDateTime.now().format(FullDateTime);
    }

    /** 格式：yyyy-MM-dd HH:mm:ss */
    public static String getDateMark() {
        return LocalDateTime.now().format(DateMark);
    }



    public static void main(String[] args) {
		System.out.println(getFullDateTime());
	}


    public synchronized static String getTime(int format)
    {
        Calendar time = Calendar.getInstance();
        return getTime(format, time);
    }

    /**
     * 得到格式化的时间
     *
     * @param format
     *            时间格式
     * @param time
     *            时间
     * @return 格式化字符串
     */
    private synchronized static String getTime(int format, Calendar time)
    {
        StringBuffer cTime = new StringBuffer(10);
        int miltime = time.get(Calendar.MILLISECOND);
        int second = time.get(Calendar.SECOND);
        int minute = time.get(Calendar.MINUTE);
        int hour = time.get(Calendar.HOUR_OF_DAY);
        int day = time.get(Calendar.DAY_OF_MONTH);
        int month = time.get(Calendar.MONTH) + 1;
        int year = time.get(Calendar.YEAR);
        if (format > 10) {
            year = year - 2000;

        }
        if (format < 20) {
            if ((format < 10))
                cTime.append(year);
            else if (format < 20)
                cTime.append(getFormatTime(year, 2));
        }
        if ((format < 20 && format > 11) || (format > 1 && format < 10))
            cTime.append(getFormatTime(month, 2));
        if ((format < 20 && format > 12) || (format > 2 && format < 10))
            cTime.append(getFormatTime(day, 2));
        if ((format > 13 && format < 20) || (format > 3 && format < 10)
                || (format > 23 && format < 30))
            cTime.append(getFormatTime(hour, 2));
        if ((format > 14 && format < 20) || (format > 4 && format < 10)
                || (format > 24 && format < 30))
            cTime.append(getFormatTime(minute, 2));
        if ((format > 15 && format < 20) || (format > 5 && format < 10)
                || (format > 25 && format < 30))
            cTime.append(getFormatTime(second, 2));
        if ((format > 16 && format < 20) || (format > 6 && format < 10)
                || (format > 26 && format < 30))
            cTime.append(getFormatTime(miltime, 3));
        return cTime.toString();
    }

    /**
     * 产生任意位的字符串
     *
     * @param time
     *            要转换格式的时间
     * @param format
     *            转换的格式
     * @return String 转换的时间
     */
    private synchronized static String getFormatTime(int time, int format) {
        StringBuffer numm = new StringBuffer();
        int length = String.valueOf(time).length();

        if (format < length) {
            return null;
        }
        for (int i = 0; i < format - length; i++) {
            numm.append("0");
        }
        numm.append(time);
        return numm.toString().trim();
    }

}

